package com.example.Lezedata.dataManager;
import androidx.lifecycle.MutableLiveData;

import com.example.Lezedata.util.GBSaveData;

import java.util.Objects;

/*登录状态*/
public class LoginManager{
    @interface LoginStatus{}
    /*未登录*/
    public static final Integer LoginStatus_normal = 0;
    /*登录中*/
    public static final Integer LoginStatus_loginIng = 1;
    /*登录成功*/
    public static final Integer LoginStatus_login = 2;
    /*登录失败*/
    public static final Integer LoginStatus_failure = 3;
    /*退出登录*/
    public static final Integer LoginStatus_exit = 3;

    public MutableLiveData  <Integer> loginStatus = new MutableLiveData<Integer>();
    private static LoginManager loginManager;
    public static LoginManager instance(){
        if (loginManager == null){
            loginManager = new LoginManager();
        }
        return loginManager;
    }

    public LoginManager(){
        String loginStatus = GBSaveData.getData("loginStatus");
        if (loginStatus == "" || loginStatus == null){
            return;
        }
        this.setLoginStatus(Integer.parseInt(loginStatus));
    }

    public void setLoginStatus(@LoginStatus Integer loginStatus) {
        if (!Objects.equals(this.loginStatus.getValue(), loginStatus)){
            GBSaveData.setData("loginStatus",String.valueOf(loginStatus));
            this.loginStatus.setValue(loginStatus);
        }
    }

    public void exitLogin(){
        setLoginStatus(LoginStatus_exit);
    }

    public Integer loginStatus(){
        return loginStatus.getValue();
    }

    public boolean isLogin(){
        return loginStatus() == LoginStatus_login;
    }
}